package com.chx.controller;

import com.chx.dto.RolePermissionDto;
import com.chx.entity.Permissions;
import com.chx.result.Result;
import com.chx.service.PermissionService;
import com.chx.vo.PermissionVo;
import com.chx.vo.RolePermissionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author cp-月入过万
 * @Date 2025/5/30
 * 权限管理接口
 */
@RestController
@RequestMapping("/admin/permission")
@Api(tags = "权限相关接口")
@Slf4j
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    /**
     * 获取所有权限
     * @return
     */
    @ApiOperation(value = "获取所有权限")
    @GetMapping("/list")
    @Cacheable(cacheNames = "permissionCache",key = "'list'")
    public Result<List<Permissions>> getAllPermission() {
        log.info("获取所有权限");
        return Result.success(permissionService.getAllPermission());
    }

    /**
     * 给角色授权
     */
    @ApiOperation(value = "给角色授权")
    @PostMapping()
    public Result<String> save(@RequestBody RolePermissionDto rolePermissionDto) {
        log.info("给角色授权");
        permissionService.save(rolePermissionDto);
        return Result.success();
    }

    /**
     * 根据角色id获取权限信息
     */
    @ApiOperation(value = "根据角色id获取权限信息")
    @GetMapping("/{id}")
    public Result<RolePermissionVo> getPermissionById(@PathVariable Long id) {
        log.info("根据角色id获取权限信息");
        return Result.success(permissionService.getPermissionById(id));
    }
}
